//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.util.*;
import javax.swing.table.*;
import org.apache.log4j.*;
import ants.p2p.utils.indexer.*;

public class SharedFilesTableModel extends AbstractTableModel{

  static Logger _logger = Logger.getLogger(StartUp.class.getName());

  ArrayList sharedFiles = new ArrayList();

  private String[] columnNames = {ji.JI.i("File Name"),
                        ji.JI.i("File Hash"),
                        ji.JI.i("File Size"),
                        ji.JI.i("File Infos"),
                        ji.JI.i("Comment")};

  public SharedFilesTableModel(ArrayList sharedFiles) {
    this.sharedFiles = sharedFiles;
  }

  public ArrayList getSharedFiles(){
    return this.sharedFiles;
  }

  public void setSharedFiles(ArrayList sharedFiles){
    this.sharedFiles = sharedFiles;
    this.fireTableRowsUpdated(0,this.sharedFiles.size());
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    if(columnIndex == 4)
      return true;
    else
      return false;
  }

  public int getRowCount() {
    if(this.sharedFiles != null)
      return this.sharedFiles.size();
    else
      return 0;
  }

  public Object getValueAt(int row, int col) {
    FileInfos fi = (FileInfos)this.sharedFiles.get(row);
    switch(col){
      case 0:
        return (new java.io.File(fi.getName())).getName();
      case 1:
        return fi.getHash();
      case 2:
        return new FileSize(fi.getSize());
      case 3:
        return fi.getExtendedInfos();
      case 4:
        return fi.getComment();
      default:
        return null;
    }
  }

  public int getColumnCount() { return columnNames.length; }

  public String getColumnName(int col) {
    return columnNames[col].toString();
  }

  public Class getColumnClass(int col) {
    try{
      switch (col) {
        case 0:
          return Class.forName("java.lang.String");
        case 1:
          return Class.forName("java.lang.String");
        case 2:
          return Class.forName("ants.p2p.utils.indexer.FileSize");
        case 3:
          return Class.forName("java.lang.String");
        case 4:
          return Class.forName("java.lang.String");
        default:
          return null;
      }
    }catch(Exception e){
      _logger.error("Cannot resolve class",e);
      return null;
    }
  }
}
